package com.muki.spider.core;

import java.util.Iterator;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class SpiderFactory implements Aggregate {
	
	private final Logger log = Logger.getLogger(SpiderFactory.class);
	private PackSpider[] spiders;
	private int last = 0;
	
	public SpiderFactory() {}
	
	public SpiderFactory(int size) {
		this.spiders = new PackSpider[size];
		log.info("create spider factory with:{ "+size+" } spiders container");
	}
	
	public PackSpider getSpider(int index) {
		return spiders[index];
	}
	
	public void appendSpider(PackSpider spider) {
		spider.put("name", "spider_"+last);
		spiders[last] = spider;
		last++;
	}
	
	public int getLength() {
		return last;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Iterator<PackSpider> iterator() {
		return new SpiderFactoryIterator(this);
	}
	
}
